/**
 * 
 * @author Sunix
 * @date   2013-5-20
 * @Email  SunixSky@gmail.com
 * 
 */	
package view.components{
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import events.AnimationEvent;
	
	import manager.LoggerManager;
	import manager.PanelManager;
	
	import scaleform.clik.controls.Button;
	import scaleform.clik.controls.Label;
	import scaleform.clik.events.IndexEvent;
	
	import sunix.animationPlayer.AnimationPlayer;
	import sunix.animationPlayer.AnimationPlayerManager;
	import sunix.animationPlayer.data.FrameData;
	
	import utils.SWFCompilerUtil;

	public class AnimationWindow extends Sprite{
		
		public var title:Label;	 //面板标题
		public var framesRate:FrameRateNumericStepper;	  //帧频设置
		public var showFrame:Sprite;     //展示窗
		public var categoryChoose:CategoryChoose;  //类别操作器
		public var animationOperator:AnimationOperator;  //动画操作器
		public var filePathOperator:FilePathOperator;   //文件操作器
		public var sdkBtn:Button;   //sdk配置路径
		
		protected var player:AnimationPlayer;   //动画播放器
		
		public function AnimationWindow(){
			addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
		}
		
		protected function onRemoveFromStage(event:Event):void{
			
		}
		
		protected function onAddedToStage(event:Event):void{
			viewInit();
			eventInit();
			x = 311;	
		}
		
		private function eventInit():void{
			framesRate.addEventListener(IndexEvent.INDEX_CHANGE,onChange);
			animationOperator.addEventListener(AnimationEvent.PREVIEW,onPreview);
			animationOperator.addEventListener(AnimationEvent.COMPILED,onCompiled);
			animationOperator.addEventListener(AnimationEvent.PLAY,onPlay);
			sdkBtn.addEventListener(MouseEvent.CLICK,onConfigSDK);
			SWFCompilerUtil.instance.addEventListener(AnimationEvent.COMPILED_FINISHED,onCompiledFinished);
		}
		
		protected function onConfigSDK(event:MouseEvent):void{
			PanelManager.showSDKOperator();
		}
		
		protected function onCompiledFinished(event:AnimationEvent):void{
			player.clear();
			categoryChoose.nameGroup();
			animationOperator.reset();
		}
		
		protected function onPlay(event:AnimationEvent):void{
			var frames:Vector.<FrameData> = animationOperator.frames;
			player.frames = frames;
			player.play();
			LoggerManager.log("播放动画");
		}
		
		protected function onCompiled(event:AnimationEvent):void{
			var name:String = categoryChoose.flashName.text;
			var fps:uint = framesRate.value;
			var outPath:String = filePathOperator.outPath.file.nativePath;
			var compressPath:String = filePathOperator.compressPath.file.nativePath;
			animationOperator.compiledFile(name,fps,outPath,compressPath);
		}
		
		protected function onPreview(event:AnimationEvent):void{
			animationOperator.getOriginalFiles(filePathOperator.resourcePath.file);
		}
		
		protected function onChange(event:Event):void{
			var value:uint = framesRate.value;
			player.fps = value;
			LoggerManager.log("更改帧频:",value);
		}
		
		private function viewInit():void{
			createPlayer();
		}
		
		protected function createPlayer():void{
			var value:uint = framesRate.value;
			player = AnimationPlayerManager.instance.getAnimationPlayer();
			player.fps = value;
			showFrame.addChild(player);
		}
	}
}